//
//  TLReadView.m
//  Books
//
//  Created by tao故乡的云 on 2017/3/13.
//  Copyright © 2017年 故乡的云. All rights reserved.
//

#import "TLReadView.h"

#define kMargin 100

@interface TLReadView ()
@property (weak, nonatomic) IBOutlet UIView *TopToolView;
@property (weak, nonatomic) IBOutlet UIView *BottonToolView;
@property (weak, nonatomic) IBOutlet UIImageView *bgImageView;
//@property (weak, nonatomic) IBOutlet UITextView *textView;
@property (nonatomic,getter=isToolHidden) bool toolHidden;
@end

@implementation TLReadView
+ (instancetype)readView {
        return [[TLReadView alloc] init];
}

- (instancetype)init {
    if (self = [super init]) {
        self = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:nil options:nil] lastObject];
        // 添加手势
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
        UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
        [self addGestureRecognizer:tap];
        [self addGestureRecognizer:pan];
        
        // 隐藏工具栏
        self.TopToolView.transform = CGAffineTransformMakeTranslation(0, -self.TopToolView.bounds.size.height);
        self.BottonToolView.transform = CGAffineTransformMakeTranslation(0, self.BottonToolView.bounds.size.height);
        self.toolHidden = YES;
    }
    
    return self;
}

- (void)setBgImage:(UIImage *)bgImage {
    _bgImage =bgImage;
    
    self.bgImageView.image = bgImage;
}

#pragma mark - 手势处理
- (void)tap:(UITapGestureRecognizer *)tap {
   // 区域判断
    CGPoint point = [tap locationInView:self];
    CGFloat x = point.x;
    CGFloat y = point.y;
    CGFloat w = self.bounds.size.width;
    CGFloat h = self.bounds.size.height;
    if (x < kMargin || (x < w - kMargin && y < kMargin)) { // 上一页
       [self previousPage] ;
        
    }else if (x > w - kMargin || y > h - kMargin){ // 下一页
        [self nextPage];

    }else { // 呼叫菜单
       [self popMenu];

    }
}

- (void)pan:(UIPanGestureRecognizer *)pan {
    static CGPoint starPoint;
    static CGPoint endPoint;
    if (pan.state == UIGestureRecognizerStateBegan) {
        starPoint = [pan locationInView:self];
    
    }else if (pan.state == UIGestureRecognizerStateEnded) {
        endPoint = [pan locationInView:self];
        (endPoint.x - starPoint.x) > 0 ? [self previousPage] : [self nextPage];
    }
    
}

- (void)previousPage {
    NSLog(@"上一页");
}

- (void)nextPage {
    NSLog(@"下一页");
}

- (void)popMenu {
    NSLog(@"菜单");
    if (self.isToolHidden) {
        [UIView animateWithDuration:0.5 animations:^{
            self.TopToolView.transform = CGAffineTransformIdentity;
            self.BottonToolView.transform = CGAffineTransformIdentity;
            self.toolHidden = NO;
        }];
    }else {
        [UIView animateWithDuration:0.5 animations:^{
            self.TopToolView.transform = CGAffineTransformMakeTranslation(0, -self.TopToolView.bounds.size.height);
            self.BottonToolView.transform = CGAffineTransformMakeTranslation(0, self.BottonToolView.bounds.size.height);
            self.toolHidden = YES;
        }];
    }
    
}

@end
